//
//  MK_HorizonShow_Layout.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/24.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_HorizonShow_Layout.h"

@interface MK_HorizonShow_Layout ()


@end


@implementation MK_HorizonShow_Layout

-(void)prepareLayout{
    self.minimumLineSpacing = 0;
    self.minimumInteritemSpacing = 0;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    CGFloat itemWidth = self.collectionView.bounds.size.width/_colNum;
    CGFloat itemHeight = self.collectionView.bounds.size.height/_rowNum;
    self.itemSize = CGSizeMake(itemWidth, itemHeight);
    self.collectionView.pagingEnabled = YES;
}

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSArray<UICollectionViewLayoutAttributes*>* origalArr = [super layoutAttributesForElementsInRect:rect];
    NSArray<UICollectionViewLayoutAttributes*>* resArr = origalArr.mutableCopy;
    
    [resArr enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.frame = [self getNeedFrameWithIndexPathItem:obj.indexPath.item];
    }];
    return resArr;
}

-(CGRect)getNeedFrameWithIndexPathItem:(NSInteger)i{
    CGFloat itemWidth = self.collectionView.bounds.size.width/_colNum;
    CGFloat itemHeight = self.collectionView.bounds.size.height/_rowNum;
    NSInteger pageNum = (NSInteger)((i)/(_colNum*_rowNum));
    NSInteger colNum = i%_colNum;
    NSInteger rowNum = (i%(_colNum*_rowNum))/_colNum;
    CGFloat itemX = colNum*itemWidth + pageNum*self.collectionView.bounds.size.width;
    CGFloat itemY = itemHeight*rowNum;
    
    return CGRectMake(itemX, itemY, itemWidth, itemHeight);
}

@end
